package com.ruizhong.centercontroll.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruizhong.centercontroll.dao.TPrebuildComponentMapper;
import com.ruizhong.centercontroll.entity.*;
import com.ruizhong.centercontroll.service.TButtonFormatService;
import com.ruizhong.centercontroll.service.TCommandService;
import com.ruizhong.centercontroll.service.TPrebuildComponentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
import org.w3c.dom.Document;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.*;

@Service
public class TPrebuildComponentServiceImpl extends ServiceImpl<TPrebuildComponentMapper, TPrebuildComponent> implements TPrebuildComponentService {

    public static Map<Integer, String> idMapType = new HashMap<Integer, String>() {
        {
            put(0, "BUTTON");
            put(1, "SWITCH");
            put(2, "SLIDER");
            put(3, "IMAGE");
            put(4, "TEXTVIEW");
            put(5, "VIDEO");
            put(6, "WEBPAGE");
            put(7,"PPT");
            put(8,"3D");
        }
    };

    @Override
    public List getPrebuildComponentByType(Integer typeNumber) {
        String type = idMapType.get(typeNumber);
        List<TPrebuildComponent> formatList = lambdaQuery()
                .eq(TPrebuildComponent::getType, type)
                .list();
        if (CollectionUtils.isEmpty(formatList)) {
            formatList = new ArrayList<>();
        }
        if (type == "BUTTON") {
            TPrebuildComponent tPrebuildComponent = new TPrebuildComponent();
            formatList.add(0, tPrebuildComponent);
            for (int i = 0; i < formatList.size(); i++) {
                TPrebuildComponent prebuildComponent = formatList.get(i);
                prebuildComponent.setW(110.0);
                prebuildComponent.setH(50.0);
                prebuildComponent.setX(50.0);
                prebuildComponent.setY(50.0);
                tPrebuildComponent.setType("BUTTON");
                prebuildComponent.setTextChinese("按钮");
                prebuildComponent.setTextTraditional("按鈕");
                prebuildComponent.setTextEnglish("button");
                prebuildComponent.setId(i);
                if (i == 0) {
                    prebuildComponent.setId(-1);
                    prebuildComponent.setBackgroundColor("white");
                    prebuildComponent.setBorderCorner(0);
                    prebuildComponent.setFontSize(15);
                    prebuildComponent.setName("经典");
                    continue;
                }

            }
            return formatList;
        }
        if (type == "SWITCH") {
            TPrebuildComponent tPrebuildComponent = new TPrebuildComponent();

            formatList.add(0, tPrebuildComponent);
            for (int i = 0; i < formatList.size(); i++) {
                TPrebuildComponent prebuildComponent = formatList.get(i);
                prebuildComponent.setW(100.0);
                prebuildComponent.setH(50.0);
                prebuildComponent.setX(50.0);
                prebuildComponent.setY(50.0);
                prebuildComponent.setType("SWITCH");
                prebuildComponent.setId(i);
                prebuildComponent.setSwitchEventEnable("false");
                if (i == 0) {
                    prebuildComponent.setId(-1);
                    prebuildComponent.setName("经典");
                }
            }
        }
        if (type == "SLIDER") {
            TPrebuildComponent tPrebuildComponent = new TPrebuildComponent();
            formatList.add(0, tPrebuildComponent);
            for (int i = 0; i < formatList.size(); i++) {
                TPrebuildComponent prebuildComponent = formatList.get(i);
                prebuildComponent.setW(80.0);
                prebuildComponent.setH(30.0);
                prebuildComponent.setX(50.0);
                prebuildComponent.setY(50.0);
                prebuildComponent.setType("SLIDER");
                prebuildComponent.setVertical(false);
                prebuildComponent.setConditionShow(false);
                prebuildComponent.setMin(0);
                prebuildComponent.setMax(100);
                prebuildComponent.setId(i);
                if (i == 0) {
                    prebuildComponent.setRailBackground("black");
                    prebuildComponent.setRailProgressColor("#FAE491");
                    prebuildComponent.setRailBorderWidth(1.0);
                    prebuildComponent.setRailBorderColor("white");
                    prebuildComponent.setRailBorderRoundCorner(5.0);
                    prebuildComponent.setSliderWidth(30.0);
                    prebuildComponent.setSliderHeight(30.0);
                    prebuildComponent.setSliderMarginTop(0.0);
                    prebuildComponent.setSliderMarginBottom(0.0);
                    prebuildComponent.setSliderMarginLeft(0.0);
                    prebuildComponent.setSliderMarginRight(0.0);
                    prebuildComponent.setSliderBackgroundColor("white");
                    prebuildComponent.setSliderBorderRadius(15.0);
                    prebuildComponent.setName("经典");
                    prebuildComponent.setId(-1);
                }
            }

        }
        if (type == "IMAGE") {
            TPrebuildComponent tPrebuildComponent = new TPrebuildComponent();
            formatList.add(0, tPrebuildComponent);
            for (int i = 0; i < formatList.size(); i++) {
                TPrebuildComponent prebuildComponent = formatList.get(i);
                prebuildComponent.setType("IMAGE");
                prebuildComponent.setW(100.0);
                prebuildComponent.setH(50.0);
                prebuildComponent.setX(50.0);
                prebuildComponent.setY(50.0);
                prebuildComponent.setId(i);
                if (i == 0) {
                    prebuildComponent.setSrc("assets/empty.png");
                    prebuildComponent.setId(-1);
                    prebuildComponent.setName("空图片");
                }
            }


        }
        if (type == "TEXTVIEW") {
            TPrebuildComponent tPrebuildComponent = new TPrebuildComponent();
            formatList.add(0, tPrebuildComponent);
            for (int i = 0; i < formatList.size(); i++) {
                TPrebuildComponent prebuildComponent = formatList.get(i);
                prebuildComponent.setType("TEXTVIEW");
                prebuildComponent.setId(i);
                prebuildComponent.setW(100.0);
                prebuildComponent.setH(50.0);
                prebuildComponent.setX(50.0);
                prebuildComponent.setY(50.0);
                prebuildComponent.setTextValue("文本");
                prebuildComponent.setTraditionalTextValue("測試文本");
                prebuildComponent.setEnglishTextValue("test text");
                if (i == 0) {
                    prebuildComponent.setId(-1);
                    prebuildComponent.setFontColor("white");
                    prebuildComponent.setFontSize(15);
                    prebuildComponent.setName("空文本");
                }
            }
        }
        if (type == "VIDEO") {
            formatList.add(new TPrebuildComponent());
            formatList.add(new TPrebuildComponent());
            formatList.add(new TPrebuildComponent());
            formatList.add(new TPrebuildComponent());
            formatList.add(new TPrebuildComponent());
            for (int i = 0; i < formatList.size(); i++) {
                TPrebuildComponent prebuildComponent = formatList.get(i);
                prebuildComponent.setType("VIDEO");
                prebuildComponent.setW(100.0);
                prebuildComponent.setH(50.0);
                prebuildComponent.setX(50.0);
                prebuildComponent.setY(50.0);
                prebuildComponent.setId(i);
                if (i == 0) {
                    prebuildComponent.setVideoType(0);
                    prebuildComponent.setName("视频");
                    prebuildComponent.setId(-1);
                }
                if (i == 1) {
                    prebuildComponent.setVideoType(1);
                    prebuildComponent.setName("流媒体（网页端）");
                    prebuildComponent.setId(-2);
                }
                if (i == 2) {
                    prebuildComponent.setVideoType(2);
                    prebuildComponent.setName("流媒体（安卓端）");
                    prebuildComponent.setId(-3);
                }
                if (i == 3) {
                    prebuildComponent.setVideoType(3);
                    prebuildComponent.setName("远程桌面 (网页端)");
                    prebuildComponent.setId(-4);
                }
                if (i == 4){
                    prebuildComponent.setVideoType(4);
                    prebuildComponent.setName("远程桌面（安卓端）");
                    prebuildComponent.setId(-5);
                }
            }

        }
        if (type == "WEBPAGE") {
            TPrebuildComponent tPrebuildComponent = new TPrebuildComponent();
            formatList.add(0, tPrebuildComponent);
            for (int i = 0; i < formatList.size(); i++) {
                TPrebuildComponent prebuildComponent = formatList.get(i);
                prebuildComponent.setType("WEBPAGE");
                prebuildComponent.setW(100.0);
                prebuildComponent.setH(50.0);
                prebuildComponent.setX(50.0);
                prebuildComponent.setY(50.0);
                prebuildComponent.setId(i);
                if (i == 0) {
                    prebuildComponent.setName("网页");
                    prebuildComponent.setId(-1);
                }
            }

        }
        if (type == "PPT") {
            TPrebuildComponent tPrebuildComponent = new TPrebuildComponent();
            formatList.add(0, tPrebuildComponent);
            for (int i = 0; i < formatList.size(); i++) {
                TPrebuildComponent prebuildComponent = formatList.get(i);
                prebuildComponent.setType("PPT");
                prebuildComponent.setW(100.0);
                prebuildComponent.setH(50.0);
                prebuildComponent.setX(50.0);
                prebuildComponent.setY(50.0);
                prebuildComponent.setId(i);
                if (i == 0) {
                    prebuildComponent.setName("PPT");
                    prebuildComponent.setId(-1);
                }
            }
        }
        if (type == "3D"){
            TPrebuildComponent tPrebuildComponent = new TPrebuildComponent();
            formatList.add(0, tPrebuildComponent);
            for (int i = 0; i < formatList.size(); i++) {
                TPrebuildComponent prebuildComponent = formatList.get(i);
                prebuildComponent.setType("3D");
                prebuildComponent.setW(100.0);
                prebuildComponent.setH(50.0);
                prebuildComponent.setX(50.0);
                prebuildComponent.setY(50.0);
                prebuildComponent.setId(i);
                if (i == 0) {
                    prebuildComponent.setName("3D场景");
                    prebuildComponent.setId(-1);
                }
            }
        }
        return formatList;
    }

    @Override
    public void savaAsFormat(Map<String, Object> tPrebuildComponentMap) {
        TPrebuildComponent tPrebuildComponent = new TPrebuildComponent();
        String type = tPrebuildComponentMap.get("type").toString();
        Object background = tPrebuildComponentMap.get("background");
        Object fontColor = tPrebuildComponentMap.get("fontColor");
        Object fontSize = tPrebuildComponentMap.get("fontSize");
        Object backgroundColor = tPrebuildComponentMap.get("backgroundColor");
        Object borderCorner = tPrebuildComponentMap.get("borderCorner");
        switch (type) {
            case "BUTTON":
                tPrebuildComponent.setType("BUTTON");
                tPrebuildComponent.setName("测试");
                tPrebuildComponent.setBackground(background == null ? null : background.toString());
                tPrebuildComponent.setFontColor(fontColor == null ? null : fontColor.toString());
                tPrebuildComponent.setFontSize(fontSize == null ? null : Integer.parseInt(fontSize.toString()));
                tPrebuildComponent.setBackgroundColor(backgroundColor == null ? null : backgroundColor.toString());
                tPrebuildComponent.setBorderCorner(borderCorner == null ? null : Integer.parseInt(borderCorner.toString()));
                break;
        }
        save(tPrebuildComponent);
    }

    @Override
    public void deleteComponentFormat(Integer id) {
        if (id < 0) {
            return;
        }
        removeById(id);
    }

    @Override
    public ResponseEntity<InputStreamResource> exportComponentTemplate(Map<String, Object> tPrebuildComponentMap) throws JAXBException {
        TPrebuildComponent tPrebuildComponent = new TPrebuildComponent();
        String type = tPrebuildComponentMap.get("type").toString();
        switch (type) {
            case "BUTTON":
                Object background = tPrebuildComponentMap.get("background");
                Object fontColor = tPrebuildComponentMap.get("fontColor");
                Object fontSize = tPrebuildComponentMap.get("fontSize");
                Object backgroundColor = tPrebuildComponentMap.get("backgroundColor");
                Object borderCorner = tPrebuildComponentMap.get("borderCorner");
                tPrebuildComponent.setType("BUTTON");
                tPrebuildComponent.setName("测试");
                tPrebuildComponent.setBackground(background == null ? null : background.toString());
                tPrebuildComponent.setFontColor(fontColor == null ? null : fontColor.toString());
                tPrebuildComponent.setFontSize(fontSize == null ? null : (Integer) fontSize);
                tPrebuildComponent.setBackgroundColor(backgroundColor == null ? null : backgroundColor.toString());
                tPrebuildComponent.setBorderCorner(borderCorner == null ? null : (Integer) borderCorner);
                JAXBContext context = JAXBContext.newInstance(TPrebuildComponent.class);
                Marshaller marshaller = context.createMarshaller();
                marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
                StringWriter sw = new StringWriter();
                marshaller.marshal(tPrebuildComponent, sw);
                InputStreamResource resource = new InputStreamResource(new ByteArrayInputStream(sw.toString().getBytes()));
                HttpHeaders headers = new HttpHeaders();
                headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=component.xml");
                return ResponseEntity.ok()
                        .headers(headers)
                        .contentLength(sw.toString().getBytes().length)
                        .contentType(MediaType.APPLICATION_XML)
                        .body(resource);
        }
        return null;
    }

    @Override
    public void ImportComponentTemplate(MultipartFile file) {
        if (file.isEmpty()) {
            throw new RuntimeException("文件内容不可为空");
        }

        try {
            // 解析XML文件
            InputStream inputStream = file.getInputStream();
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(inputStream);

            // 假设XML结构为 <myObject><name>...</name><value>...</value></myObject>
            String background = document.getElementsByTagName("background").item(0).getTextContent();
            String borderCorner = document.getElementsByTagName("borderCorner").item(0).getTextContent();
            String fontColor = document.getElementsByTagName("fontColor").item(0).getTextContent();
            String fontSize = document.getElementsByTagName("fontSize").item(0).getTextContent();
            String name = document.getElementsByTagName("name").item(0).getTextContent();
            String type = document.getElementsByTagName("type").item(0).getTextContent();
            TPrebuildComponent tPrebuildComponent = new TPrebuildComponent();
            tPrebuildComponent.setBackground(background);
            tPrebuildComponent.setBorderCorner(Integer.parseInt(borderCorner));
            tPrebuildComponent.setFontColor(fontColor);
            tPrebuildComponent.setFontSize(Integer.parseInt(fontSize));
            tPrebuildComponent.setName(name);
            tPrebuildComponent.setType(type);
            save(tPrebuildComponent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
